package com.future.mayi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.utils.ToastUtils;
import com.future.mayi.R;
import com.future.mayi.utils.HtmlFormat;

import org.json.JSONException;
import org.json.JSONObject;

public class InstructionsDialog extends Dialog {
    private Context context;

    public InstructionsDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.context = context;
        View layout = LayoutInflater.from(context).inflate(R.layout.dialog_instructions, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) ( CommonUtils.getScreenSize(context)[0] * 0.9f), ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(layout, params);
        findViewById(R.id.tv_enter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        new HttpRequest<DefaultResponse>()
                .with(context)
                .setApiCode(ApiCst.chengseshuoming)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        String responseString;
                        try {
                            JSONObject object = new JSONObject(response.content);
                            responseString = object.optString("info");
                            ((WebView) findViewById(R.id.web_view)).loadDataWithBaseURL(null, HtmlFormat.getNewContent(responseString), "text/html", "utf-8", null);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        ToastUtils.shortToast(InstructionsDialog.this.context, message);
                    }
                })
                .start(new DefaultResponse());

    }
}
